package com.gjs.freechat.server.business;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.gjs.freechat.server.component.ChatSessionManage;
import com.gjs.freechat.server.component.UserManage;
import com.gjs.freechat.server.component.UserMatchManage;
import com.gjs.freechat.server.component.UserSessionPool;
import com.gjs.freechat.server.component.UsersPoolManage;
import com.gjs.freechat.server.component.bean.Conversation;
import com.gjs.freechat.server.component.bean.UserInfo;
import com.gjs.freechat.server.dao.UserDao;
import com.gjs.freechat.server.dao.mapper.UserInfoMapper;
import com.gjs.freechat.utils.SHA1;
import com.gjs.freechat.web.constant.WebConstants;

@Service
public class UserManageBusinessImpl implements UserManageBusiness {
	
	@Autowired
	private UserDao userDao;

	@Autowired
	private UserInfoMapper userInfoMapper;
	
	@Autowired
	private UserManage userManage;
	
	@Autowired
	private ChatSessionManage chatSessionManage;
	
	
	@Autowired
	private UserSessionPool sessionPool;
	
	@Autowired
	private ChatSessionBusiness chatSessionManageBusiness; 
	
	@Autowired
	UsersPoolManage usersPoolManage;
	
	@Autowired
	private UserMatchManage userMatchManage;
	
	/**
	 * 用户手动登录验证
	 * @return 验证结果，结果包括结果代码以及用户信息
	 * 
	 */
	@Override
	public Map<String, Object> login(String email, String password) {
		Map<String, Object> result = new HashMap<String, Object>();
		
		UserInfo userInfo = userDao.getUserInfoByUserNameOrEmail(email);
		
		if (userInfo == null) {
			result.put("resultCode", WebConstants.LOGIN_RESULT_USERNAME_ERROR);
			return result;
			
		}
		if (!password.equals(userInfo.getPassword())) {
			result.put("resultCode", WebConstants.LOGIN_RESULT_PASSWORD_ERROR);
			return result;
		}
		
		//System.out.println(userInfo);
		
		loginRegister(userInfo);     //将用户登录状态写入到数据库
		//userInfo.setPassword(null);
		result.put("resultCode", WebConstants.LOGIN_RESULT_SUCCESS);
		result.put("userInfo", userInfo);
		
		return result;
	}
	
	@Override
	public void logout(UserInfo userInfo) {
		logoutRegister(userInfo);
		chatSessionManageBusiness.userLoginStatusMessagePush(userInfo.getId(), UserInfo.USER_STATUS_OFFLINE);
		chatSessionManageBusiness.onlineUsersCountMsgPush();
		chatSessionManageBusiness.stopAllMatch(userInfo.getId());
		userMatchManage.remove(userInfo);
		sessionPool.removeConvos(userInfo.getId());
		usersPoolManage.remove(userInfo);
	}
	
	/**
	 * 将用户已经登录的信息写入数据库中
	 */
	@Override
	public void loginRegister(UserInfo userInfo) {
		userInfo.setLogined(1);
		userInfoMapper.loginRegister(userInfo.getId());
		usersPoolManage.add(userInfo);
		sessionPool.registerConvos(userInfo.getId(), chatSessionManage.getAllConvos(userInfo.getId()));
		chatSessionManageBusiness.userLoginStatusMessagePush(userInfo.getId(), UserInfo.USER_STATUS_ONLINE);
		chatSessionManageBusiness.onlineUsersCountMsgPush();
	}
	
	@Override
	public void logoutRegister(UserInfo userInfo) {
		userInfoMapper.logoutRegister(userInfo.getId());
		//usersPoolManage.remove(userInfo);
	}
	
	/**
	 * 创建新的用户
	 */
	@Override
	public UserInfo createNewUser() {
		UserInfo userInfo = userManage.createNewUser();
		UserInfo admin = userDao.getUserInfoByUserName("team");
		chatSessionManage.createNewChatSession(UUID.randomUUID().toString(), Conversation.CONVO_TYPE_NORMAL, userInfo, admin);
		return userInfo;
	}
	
	@Override
	public void updateUserInfo(UserInfo userInfo) {
		userInfoMapper.updateUserInfo(userInfo);
	}
	
	/**
	 * 将用户输入的邮箱和密码存入数据库中
	 */
	@Override
	public boolean userRegister(String userId, String email, String password) {
		try {
			return userInfoMapper.userRegister(userId, email, password);
		} catch (Exception e) {
			//e.printStackTrace();
		}
		return false;
	}
	
	@Override
	public boolean editUsername(String userId, String username) {
		try {
			return userInfoMapper.editUsername(userId, username);
		} catch (Exception e) {
		}
		return false;
	}
	
	@Override
	public void editPassword(String userId, String newPass) {
		userInfoMapper.updatePassword(userId, newPass);
	}
}
